package com.live.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.live.model.Admin;
import com.live.model.Contract;
import com.live.service.ContractService;

@WebServlet("/contractList")
public class ContractListController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int pageSize = 10;
	private ContractService  contractService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		contractService =new ContractService();
		String firstPartyName = request.getParameter("firstPartyName").trim();
		int pageIndex = 1;
		if (null != request.getParameter("pageIndex") && Integer.parseInt(request.getParameter("pageIndex").trim())>0) {
			pageIndex = Integer.parseInt(request.getParameter("pageIndex").trim());
		}
		try {
			HttpSession session = request.getSession(true);
			Admin admin = (Admin) session.getAttribute("CurrentUser");
			if(admin !=null){
			if (firstPartyName!=null) {
				List<Contract>  contractsList = contractService.getPageContract(firstPartyName, pageIndex, pageSize);
				request.setAttribute("contractsList", contractsList);// 传出contractsList
				int totalCount = contractService.getTotalCount(firstPartyName);
				request.setAttribute("totalCount", totalCount);
				int totalPage = contractService.getTotalPage(firstPartyName, pageSize);
				request.setAttribute("firstPartyName", firstPartyName);
				request.setAttribute("totalPage", totalPage);
				request.setAttribute("pageIndex", pageIndex);
			}else{
				List<Contract>  contractsList = contractService.getPageContract("", pageIndex, pageSize);
				request.setAttribute("contractsList", contractsList);
				int totalCount = contractService.getTotalCount("");
				request.setAttribute("totalCount", totalCount);
				int totalPage = contractService.getTotalPage("", pageSize);
				request.setAttribute("totalPage", totalPage);
				request.setAttribute("pageIndex", pageIndex);
				}
				request.getRequestDispatcher("WEB-INF/view/contractList.jsp").forward(request,
					response);
			}else {
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
				}
			} catch (Exception e) {
				e.printStackTrace();
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
			}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		contractService =new ContractService();
		String firstPartyName = request.getParameter("firstPartyName").trim();
		int pageIndex = 1;
		if (null != request.getParameter("pageIndex") && Integer.parseInt(request.getParameter("pageIndex").trim())>0) {
			pageIndex = Integer.parseInt(request.getParameter("pageIndex").trim());
		}
		try {
			HttpSession session = request.getSession(true);
			Admin admin = (Admin) session.getAttribute("CurrentUser");
			if(admin !=null){
			List<Contract>  contractsList = contractService.getPageContract(firstPartyName, pageIndex, pageSize);
			request.setAttribute("contractsList", contractsList);// 传出newsList
			int totalCount = contractService.getTotalCount(firstPartyName);
			request.setAttribute("totalCount", totalCount);
			int totalPage = contractService.getTotalPage(firstPartyName, pageSize);
			request.setAttribute("firstPartyName", firstPartyName);
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("pageIndex", pageIndex);
			request.getRequestDispatcher("WEB-INF/view/contractList.jsp").forward(request,
				response);
			
			}else {
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
				}
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("login.jsp").forward(request,
					response);
		}
	}
}

